package com.dao.wmsdao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bean.wms.OrderDetail;
import com.bean.wms.Orders;
import com.dao.BaseDao;
import com.dao.wmsdao.OrdersDao;
import com.dao.wmsdao.VerifyDao;
import com.domain.beanutil.DataUtilInterface;
import com.ibatis.sqlmap.client.SqlMapExecutor;

/** 
* @ClassName: OrdersDaoImpl 
* @Description: TODO 
* @author wangxm
* @date 2014-3-26 闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺�鐎氾拷:06:34 
*  
*/
@Repository("ordersDao")
@SuppressWarnings("unchecked")
@Transactional(rollbackFor=Exception.class)
public class OrdersDaoImpl extends BaseDao implements OrdersDao {
	
	@Autowired
	private VerifyDao verifyDao;

	@Override
	public Orders getOrdersByExternOrderskey(String wareHourse, String externorderkey , String storerkey) throws SQLException {
		
		Map<String, String> map = new HashMap<String, String>();
		
		if(wareHourse == null || "".equals(wareHourse)){
			wareHourse = DataUtilInterface.defaultwhseid;
		}
		
		map.put("warehourse", wareHourse);
		map.put("externorderkey", externorderkey);
		map.put("storerkey", storerkey);	
		return (Orders) getSqlMapClient().queryForObject(Orders.class.getName()+".getOrdersByExternOrderskey", map);
	}

	@Override
	public void insertOrders(Orders orders) throws SQLException {
		if(orders.getWhseid() == null || "".equals(orders.getWhseid())){
			orders.setWhseid(DataUtilInterface.defaultwhseid);
		}
		getSqlMapClient().insert(orders.getClass().getName()+".insertOrders", orders);
	}

	@Override
	public void insertOrderDetail(OrderDetail orderDetail) throws SQLException {
		if(orderDetail.getWhseid() == null || "".equals(orderDetail.getWhseid())){
			orderDetail.setWhseid(DataUtilInterface.defaultwhseid);
		}
		
    	getSqlMapClient().insert(Orders.class.getName()+".insertOrderDetail", orderDetail);
	}

	@Override
	public void updateOrders(Orders orders) throws SQLException {
		getSqlMapClient().update(orders.getClass().getName()+".update", orders);
	}

	@Override
	public void updateOrderDetail(OrderDetail orderDetail) throws SQLException {
		getSqlMapClient().update(Orders.class.getName()+".updatedetail", orderDetail);
	}


	@Override
	public List<Orders> getOrdersByStatus(String status , String susr1 , String susr3){
		List<String> dbList = getSqlMapClientTemplate().queryForList("Verify.selectdb");
		
		List<Orders> ordersList = new ArrayList<Orders>();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("status", status);
		map.put("susr1", susr1);
		map.put("susr3", susr3);
		
		for (String warehourse : dbList) {
			
			if("enterprise".equals(warehourse.trim().toLowerCase())){
				continue;
			}
			
			map.put("warehourse", warehourse);
			List<Orders> ordersList2 = getSqlMapClientTemplate().queryForList(Orders.class.getName()+".getOrdersByStatus", map);
			ordersList.addAll(ordersList2);
		}
	    return ordersList;
	}

	@Override
	public void deleteOrderDetailByOrderKey(String wareHourse ,String orderKey,String externOrderKey) throws SQLException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("orderKey", orderKey);
		map.put("externOrderKey",externOrderKey);
		if(wareHourse == null || "".equals(wareHourse)){
			wareHourse = DataUtilInterface.defaultwhseid;
		}
		map.put("warehourse", wareHourse);
		
		getSqlMapClient().delete(Orders.class.getName()+".deleteOrderDetail");
	}

	@Override
	public void insertComOrders(Orders orders) throws SQLException {
		this.insertOrders(orders);
		for (OrderDetail orderDetail : orders.getItemList()) {
			orderDetail.setOrderkey(orders.getOrderkey());
			Map<String, String> map = verifyDao.getAllocatestrategykey(DataUtilInterface.defaultwhseid, orderDetail.getSku(), orderDetail.getStorerkey());
			orderDetail.setAllocatestrategykey(map.get("ALLOCATESTRATEGYKEY"));
			orderDetail.setPreallocatestrategykey(map.get("PREALLOCATESTRATEGYKEY"));
			this.insertOrderDetail(orderDetail);
		}
	}

	@Override
	public void insertRecordByList(final List<Orders> ordersList) throws SQLException {
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
		      public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
			        executor.startBatch();

			        final int batchSize = 500;
			        int count = 0;
			        
			        for (Orders record : ordersList) {
				          executor.insert(record.getClass().getName()+".insertOrders", record);
				          
				          for (OrderDetail orderDetail : record.getItemList()) {
				        	  orderDetail.setOrderkey(record.getOrderkey());
					  		  Map<String, String> map = verifyDao.getAllocatestrategykey(DataUtilInterface.defaultwhseid, orderDetail.getSku(), orderDetail.getStorerkey());
					  		  orderDetail.setAllocatestrategykey(map.get("ALLOCATESTRATEGYKEY"));
					  		  orderDetail.setPreallocatestrategykey(map.get("PREALLOCATESTRATEGYKEY"));
				        	  executor.insert(record.getClass().getName()+".insertOrderDetail", orderDetail);
						  }
				          	
				          if (++count % batchSize == 0) {
				            executor.executeBatch();
				          }
			        }
			        executor.executeBatch();
			        return null;
		      }
		});
	}

	@Override
	public List<Orders> getOrdersCancelByStatus(String susr1, String susr3,String cancelmark, String cancelcomplete) {
		
		List<String> dbList = getSqlMapClientTemplate().queryForList("Verify.selectdb");
		
		List<Orders> ordersList = new ArrayList<Orders>();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("susr1", susr1);
		map.put("susr3", susr3);
		map.put("cancelmark", cancelmark);
		map.put("cancelcomplete", cancelcomplete);
		//map.put("PBSHIP",PBSHIP);
		
		for (String warehourse : dbList) {
			
			if("enterprise".equals(warehourse.trim().toLowerCase())){
				continue;
			}
			
			map.put("warehourse", warehourse);
			List<Orders> ordersList2 = getSqlMapClientTemplate().queryForList(Orders.class.getName()+".getOrdersCancelByStatus", map);
			ordersList.addAll(ordersList2);
		}
	    return ordersList;
	}
}
